package cn.bouncyslime.graduationProject.nba.crawler.spider;

import cn.bouncyslime.graduationProject.nba.dto.TeamDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 张一卓
 * @date 2020/4/17 上午2:23
 **/
public class OldTeamPageProcessor implements PageProcessor {

    private static final Logger logger = LoggerFactory.getLogger(NewTeamPageProcessor.class);

    /**
     * nba数据统计网站
     */
    private static final String BASE_URL = "http://www.stat-nba.com";

    /**
     * 爬取首页
     */
    private static final String START_URL = "http://www.stat-nba.com/teamList.php";

    /**
     * 球队页面匹配规则
     */
    private static String regex = "\\.(/([a-zA-Z]+)/([a-zA-Z]+).([a-zA-Z]+))";

    private static Pattern pattern = Pattern.compile(regex);

    private Site site = Site.me().setSleepTime(1500).setRetryTimes(3).setRetrySleepTime(1000).setCharset("utf-8")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.369")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
            .addHeader("Connection", "keep-alive")
            .addHeader("Host", "www.stat-nba.com")
            .addCookie("__cfduid", "def62db9852e6b626f3d744bacadd235a1585654597")
            .addCookie("Hm_lvt_102e5c22af038a553a8610096bcc8bd4", "1585654597")
            .addCookie("yjs_id", "aHR0cHM6Ly93d3cuYmFpZHUuY29tL2xpbms/dXJsPXlGVU92Y0NMMERvR3ZUT01SVm1kS05NeExSeDNEWHFxcklfakxDSXVpMHJxY1E0RkJITTFncEhGX2lQcHJ2ekgmd2Q9JmVxaWQ9ZGM3OTgyN2YwMDczMjg1OTAwMDAwMDAzNWU4MzJiNDN8MTU4NTY1NDU5Njg0Ng")
            .addCookie("ctrl_time", "1")
            .addCookie("Hm_lpvt_102e5c22af038a553a8610096bcc8bd4", "1585672286");

    @Override
    public void process(Page page) {
        String pageUrl = page.getUrl().toString();
        if (page.isDownloadSuccess()) {
            Html html = page.getHtml();
            // 当前爬取页面是球队列表页面时
            if (START_URL.equals(pageUrl)) {
                List<String> teamUrlList = html.xpath("div[@class='teamList']/div[@class='team']/a/@href").all();
                List<String> teamCNameList = html.xpath("div[@class='teamList']/div[@class='team']/a/div/text()").all();
                // 构建各个球队详情页面的url，并获取部分球队数据
                for (int j = 0; j < teamUrlList.size(); j++) {
                    String teamUrl = teamUrlList.get(j);
                    Matcher matcher = pattern.matcher(teamUrl);
                    if (matcher.find()){
                        String suffixTeamUrl = matcher.group(1);
                        String url = BASE_URL + suffixTeamUrl;
                        Request request = new Request(url);
                        request.putExtra("teamId", matcher.group(3)).putExtra("teamCName", teamCNameList.get(j));
                        page.addTargetRequest(request);
                    }
                }
            } else {    // 当前爬取页面是球队详情页面时
                // 球队英文名称列表
                String teamEName = html.xpath("div[@class='intro']//div[@class='row']/div[@class='value']/text()").get().split("\\(")[0];
                Request request = page.getRequest();
                String teamId = request.getExtra("teamId").toString();
                String teamCName = request.getExtra("teamCName").toString();
                TeamDto teamDto = new TeamDto(teamId, teamEName, teamCName, null, null, 1);
                page.putField("team", teamDto);
            }
        } else {
            logger.warn("" + pageUrl + "页面爬取失败");
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new OldTeamPageProcessor()).addUrl("http://www.stat-nba.com/teamList.php").addPipeline(new ConsolePipeline()).thread(5).run();
    }
}
